<?php
namespace app\tbk\controller;
use x\my\Pay;
use ETaobao\Factory;
class Admin extends Base{
    public function __construct(){
        parent::__construct();
        $this->checkLogin();
        $end_time = m('Userxiangmu','c')->check($this->userinfo['id'], 1);
        if(!$end_time){
          r(url('bbs/userxiangmu/pay',['id'=>1]));
        }
        $this->assign('page_title','淘宝券');
    }
    //首页
    public function index(){
      $this->fetch([],'','layadmin');
    }

    //列表
    public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map = [];
            $map[] = ['and','user_id', '=',$this->userinfo['id']];
            if ($d['keyword'] != '') {
                $map[] = ['and', 'favorites_title', 'like', "%{$d['keyword']}%"];
            }
            $w = where($map);
            $rs = $this->db->query("select * from tbk_cat $w[0] order by sort desc, id asc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                    $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
        }
        $this->fetch([],'','layadmin');
    }

    //申请
    public function apply(){
      $xpk = $this->getxpk();
      if(is_post()){
          $d = $this->post;
          $rule = [
            'tbk_app_key'=>'require',
            'tbk_app_secret'=>'require',
            'tbk_adzone_id' => 'require',
          ];
          $msg = [
          ];
          check($rule,$msg,$d);
          $data = [
            'id' => $this->userinfo['id'],
            'tbk_app_key' => $d['tbk_app_key'],
            'tbk_app_secret' => $d['tbk_app_secret'],
            'tbk_adzone_id' => $d['tbk_adzone_id'],
          ];
          $this->db->update('user', $data);
          $this->db->query("delete from tbk_cat where user_id=?", [$this->userinfo['id']]);
          $this->db->query("delete from tbk_item where user_id=?", [$this->userinfo['id']]);
          $temp = [];
          foreach($xpk as $v){
            $arr = [
              'user_id' => $this->userinfo['id'],
              'favorites_title' => $v['favorites_title'],
              'favorites_id' => $v['favorites_id'],
              'adzone_id' => $d['tbk_adzone_id'],
              'create_time' => time(),
            ];
            $temp[] = $arr;
          }
          $this->db->insertAll('tbk_cat', $temp);
          cookie('js_msg','更新成功');
          cookie('js_function','f5');
          suc();
      }
      $txt = get_http_type() . DOMAIN . '/vtbk/index.html#/index?uid='.$this->userinfo['id'];
      $txt = urlencode($txt);
      $img_url = url('bbs/com/qrcode',['txt'=>$txt]);
      $this->fetch(['xpk'=>$xpk, 'img_url'=>$img_url],'','layadmin');
    }
    //上传root文件
    public function upfile(){
      if(is_post()){
        $file = file_get_contents($_FILES['file']['tmp_name']);
        file_put_contents('root.txt',$file);
        suc();
      }
    }

    //微信支付页面, 扫码支付
    public function wxpay(){
      if(is_post()){
          $d = $this->post;
          $rule = [
              'year'=>'require',
          ];
          $msg = [
          ];
          check($rule,$msg,$d);

          $order_no = $this->com_order_no();
          $money = $d['year']==100 ? 220 : 200;
          $fee = bcmul($money, c('type.fee'),2);
          $real_money = bcsub($money, $fee, 2);
          $data = [
            'order_no' => $order_no,
            'user_id' => $this->userinfo['id'],
            'year' => $d['year'],
            'type' => 0,
            'money' => $money,
            'fee' => $fee,
            'real_money' => $real_money,
            'create_time' => time(),
          ];
          $id = $this->db->insert('order_xiangmu', $data);

          //微信支付
          $con = c('api.wx_gzh_config.myb');
          $config = [
              'app_id'  => $con['app_id'],
              'mch_id'  => $con['mch_id'],
              'key'     => $con['key'],
              'mode'    => 'optional',
              'notify_url' => url('notice/wxpay'),
          ];
          $pay = Pay::wechat($config);
          $order = [
              'out_trade_no' => $order_no,
              'body' => '购买蚂蚁帮在线服务',
              'total_fee' => bcdiv($money,100),
          ];
          $rs = $pay->scan($order);

          $txt = urlencode($rs->code_url);
          $url = url('com/qrcode',['txt'=>$txt]);
          suc($url);


          //微信支付
          // $shop = c('api.wx_gzh_config.myb');
          // $config = [
          //     'app_id'  => $shop['app_id'],
          //     'mch_id'  => $shop['mch_id'],
          //     'key'     => $shop['key'],
          //     'mode'    => 'optional',
          //     'notify_url' => url('notice/wxpay'),
          // ];
          // $pay = Pay::wechat($config);
          // $order = [
          //     'out_trade_no' => $order_no,
          //     'body' => '购买蚂蚁帮在线服务',
          //     'total_fee' => $money,
          // ];
          // $rs = $pay->wap($order)->getcontent();
          // exit($rs);
      }
      $this->fetch();
    }

    //上传联系方式
    public function connect(){
      if(is_post()){
          $d = $this->post;
          $rule = [
              'id' => 'require',
              'pic'=>"require",
          ];
          $msg = [
              'pic.require' => '请上传微信二维码',
          ];
          check($rule,$msg,$d);
          $data=[
            'id'=>$d['id'],
            'pic_wx_qrcode' => $d['pic'],
            'wxid' => $d['wxid'],
            'update_time' => time(),
          ];
          $this->db->update('user',$data);
          m('Attachment','xcom')->imgSure('user',$data['id'],$data);
          cookie('js_msg','更新成功');
          cookie('js_function','preload');
          suc();
      }
      $this->fetch([],'','layadmin');
    }

    //选品库
    public function getxpk(){
      $xpk = [];
      $config = [
          'appkey' => $this->userinfo['tbk_app_key'],
          'secretKey' => $this->userinfo['tbk_app_secret'],
          'format' => 'json',
          'sandbox' => false,
      ];
      $app = Factory::Tbk($config);
      $param = [
         'fields' => "favorites_title,favorites_id,type",
         'page_size'=>100,
         'page_no'=>1,
      ];
      $res = $app->uatm->getFavorites($param);
      $res = json_encode($res);
      $res = json_decode($res,true);
      $xpk = isset($res['results']) ? $res['results']['tbk_favorites'] : [];
      return $xpk;
    }
}
?>
